﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Web.Utility;
using Portal.Systems.Model;
using Portal.Web.Model;

namespace Portal.Systems.Render
{
    public class RoleInGroupRoleRenderBll
    {
        static void CheckPermission(string SiteId)
        {
            try
            {
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, SystemPermission.StaticId, SystemPermission.funcManage, SystemPermission.funcManage, UserId, true);
            }
            catch (Exception ex)
            {
                string ReturnData = "<div class=CssAccessDenied>" + ex.Message.ToString() + " <span class=CssBack><a href=\"javascript:history.back(-1);\">Quay lại</a></span></div>\r\n";
                throw new Exception(ReturnData);
            }
        }

        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CheckPermission(SiteId);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string HttpButtonUrl = WebConfig.GetButtonIconRoot(OSiteParam);
                string GroupRoleId = (string)WebEnvironments.Request(SiteId, "GroupRoleId");
                GroupRoleCls OGroupRole = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CreateGroupRoleModel(SiteId, GroupRoleId);
                if (OGroupRole == null) throw new Exception("Nhóm vị trí công việc không hợp lệ!");
                string Html =
                   "<script language=javascript>\r\n" +
                   "    function ReadingAvailable()\r\n"+
                   "    {\r\n"+
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        GroupRoleId=document.getElementById('txtGroupRoleId').value;\r\n" +
                   "        AvailableKeyword=document.getElementById('txtKeywordAvailable').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.RoleInGroupRoleRenderBll.ServerSideReadingAvailableRole(SiteId,GroupRoleId, AvailableKeyword).value;\r\n" +
                   "        document.getElementById('divAvailableRole').innerHTML=AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n"+

                   "    function ReadingBelong()\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        GroupRoleId=document.getElementById('txtGroupRoleId').value;\r\n" +
                   "        BelongKeyword=document.getElementById('txtKeywordBelong').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.RoleInGroupRoleRenderBll.ServerSideReadingBelongRole(SiteId,GroupRoleId, BelongKeyword).value;\r\n" +
                   "        document.getElementById('divBelongRole').innerHTML=AjaxOut.HtmlContent;\r\n" +
                   "    }\r\n" +

                   "    function ClientAddRoleToGroupRole(RoleId)\r\n"+
                   "    {\r\n"+
                   "        SiteId='"+SiteId+"';\r\n"+
                   "        GroupRoleId=document.getElementById('txtGroupRoleId').value;\r\n" +
                   "        AvailableKeyword=document.getElementById('txtKeywordAvailable').value;\r\n" +
                   "        BelongKeyword=document.getElementById('txtKeywordBelong').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.RoleInGroupRoleRenderBll.ServerSideAddRoleToGroupRole(SiteId,GroupRoleId,RoleId, AvailableKeyword, BelongKeyword).value;\r\n" +
                   "        document.getElementById('divAvailableRole').innerHTML=AjaxOut.HtmlContent;\r\n"+
                   "        document.getElementById('divBelongRole').innerHTML=AjaxOut.RetExtraParam1;\r\n" +
                   "    }\r\n"+

                   "    function ClientRemoveRoleFromGroupRole(RoleId)\r\n" +
                   "    {\r\n"+
                   "        SiteId='"+SiteId+"';\r\n"+
                   "        if(confirm('Bạn đã chắc chắn loại bỏ chưa?')==false)return;\r\n"+
                   "        GroupRoleId=document.getElementById('txtGroupRoleId').value;\r\n" +
                   "        AvailableKeyword=document.getElementById('txtKeywordAvailable').value;\r\n" +
                   "        BelongKeyword=document.getElementById('txtKeywordBelong').value;\r\n" +
                   "        AjaxOut = Portal.Systems.Render.RoleInGroupRoleRenderBll.ServerSideRemoveRoleFromGroupRole(SiteId,GroupRoleId, RoleId, AvailableKeyword, BelongKeyword).value;\r\n" +
                   "        document.getElementById('divBelongRole').innerHTML=AjaxOut.HtmlContent;\r\n" +
                   "        document.getElementById('divAvailableRole').innerHTML=AjaxOut.RetExtraParam1;\r\n" +
                   "    }\r\n"+
                   "</script>\r\n" +
                   "<input type=hidden id=txtGroupRoleId value=\"" + GroupRoleId + "\">\r\n" +
                   "<div class=FormFunction id=divListForm>\r\n" +
                   "   <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:window.open('GroupRole.aspx','_self');\"><img class=image src=\""+HttpButtonUrl+"/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <h3>Thiết lập cho nhóm: " + OGroupRole.GroupRoleName + "</div>\r\n" +
                   "  <div class=CssListForm>\r\n" +
                   "  <table>\r\n" +
                   "    <tr>\r\n" +
                   "        <td class=col1>\r\n" +
                   "            <div class=CssFilter>Từ khóa: <input id=\"txtKeywordAvailable\"><input type=button class=button value=\"Lọc dữ liệu\" onclick=\"javascript:ReadingAvailable();\"></div>\r\n" +
                   "            <div id=divAvailableRole>" + ServerSideReadingAvailableRole(SiteId, GroupRoleId,"").HtmlContent + "</div>\r\n" +
                   "        </td>\r\n" +
                   "        <td class=col1>\r\n" +
                   "            <div class=CssFilter>Từ khóa: <input id=\"txtKeywordBelong\"><input type=button class=button value=\"Lọc dữ liệu\" onclick=\"javascript:ReadingBelong();\"></div>\r\n" +
                   "         <div id=divBelongRole>" + ServerSideReadingBelongRole(SiteId, GroupRoleId,"").HtmlContent + "</div>\r\n" +
                   "        </td>\r\n" +
                   "    </tr>\r\n" +
                   "  </table>\r\n" +
                   " </div>\r\n"+
                   "</div>\r\n";
                   
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = "<div class=CssAccessDenied>"+ ex.Message.ToString()+"</div>\r\n";
            }
            
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideReadingAvailableRole(string SiteId, string GroupRoleId,string Keyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                RoleCls[] AvailableRoles = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingRoleAvailableForGroupRole(SiteId, GroupRoleId, Keyword);
                string Html =
                    "<h3>Danh sách vị trí công việc</h3>\r\n" +
                    "<div class=tabcontent>\r\n" +
                    "   <table>\r\n" +
                    "    <tr>\r\n" +
                    "       <th class=thIndex>#</th>\r\n" +
                    "       <th>Vị trí công việc</th>\r\n" +
                    "       <th></th>\r\n" +
                    "   </tr>\r\n";
                for (int iIndex = 0; iIndex < AvailableRoles.Length; iIndex++)
                {
                    Html +=   " <tr>\r\n" +
                              "     <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                              "     <td>" + AvailableRoles[iIndex].RoleName + "</td>\r\n" +
                              "     <td class=tdButton><input type=button class=button value=\"Đưa vào\" onclick=\"javascript:ClientAddRoleToGroupRole('"+AvailableRoles[iIndex].RoleId+"');\"></td>\r\n" +
                              " </tr>\r\n";
                }

                Html +=
                    "</table>\r\n"+
                    "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideReadingBelongRole(string SiteId, string GroupRoleId,string Keyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                RoleCls[] RoleBelongRoles = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingRoleBelongGroupRole(SiteId, GroupRoleId, Keyword);
                string Html =
                    "<h3>Danh sách vị trí công việc thuộc nhóm</h3>\r\n" +
                    "<div class=tabcontent>\r\n" +
                    "   <table>\r\n" +
                    "    <tr>\r\n" +
                    "       <th class=thIndex>#</th>\r\n" +
                    "       <th>Tài khoản</th>\r\n" +
                    "       <th></th>\r\n" +
                    "   </tr>\r\n";
                for (int iIndex = 0; iIndex < RoleBelongRoles.Length; iIndex++)
                {
                    Html += " <tr>\r\n" +
                              "     <td class=tdIndex>" + (iIndex + 1) + "</td>\r\n" +
                              "     <td>" + RoleBelongRoles[iIndex].RoleName + "</td>\r\n" +
                              "     <td><input type=button class=button value=\"Loại ra\" onclick=\"javascript:ClientRemoveRoleFromGroupRole('" + RoleBelongRoles[iIndex].RoleId + "');\"></td>\r\n" +
                              " </tr>\r\n";
                }

                Html +=
                    "</table>\r\n" +
                    "</div>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }



        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideAddRoleToGroupRole(
            string SiteId, 
            string GroupRoleId, 
            string RoleId,
            string AvailableKeyword,
            string BelongKeyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).AddRoleToGroupRole(null, SiteId, GroupRoleId, RoleId);
                OAjaxOut.HtmlContent = ServerSideReadingAvailableRole(SiteId, GroupRoleId, AvailableKeyword).HtmlContent;
                OAjaxOut.RetExtraParam1 = ServerSideReadingBelongRole(SiteId, GroupRoleId, BelongKeyword).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public static AjaxOut ServerSideRemoveRoleFromGroupRole(
            string SiteId,
            string GroupRoleId, 
            string RoleId,
            string AvailableKeyword,
            string BelongKeyword)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).RemoveRoleFromGroupRole(null, SiteId, GroupRoleId, RoleId);
                OAjaxOut.HtmlContent = ServerSideReadingBelongRole(SiteId, GroupRoleId,BelongKeyword).HtmlContent;
                OAjaxOut.RetExtraParam1 = ServerSideReadingAvailableRole(SiteId, GroupRoleId,AvailableKeyword).HtmlContent;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
    }
}
